<?php
if ('itg-em-admin-list-atd.php' == basename($_SERVER['SCRIPT_FILENAME']))
    die ('<h2>Direct File Access Prohibited</h2>');
if(!current_user_can('itg_em_cap_admin')) {
    wp_die('Cheating eh? No luck if Swashata is the developer ;)');
    return;
}

/**
 * This file deals with the printable table list for all attendees
 * Lists down Attendees grouped by their uid
 * Uses AJAX interface to not to hang the server
 * @param string $start_date The date from where to look (inclusive)
 * @param string $end_date The date upto which to look (inclusive)
 * @uses itg_em_admin_ajax_list_atd_cb to fetch details
 * First get the ids using get_col
 * Then contruct a JS literal array
 * Then do the magic
 * Using the same AJAX interaction as Reg. Thumbnail plugin Hat Tip: Viper007bond <http://plugins.trac.wordpress.org/browser/regenerate-thumbnails/trunk/regenerate-thumbnails.php?rev=258798>
 */
/** Get Globals */
global $wpdb, $itg_em_db_table_name;
?>
<div class="wrap">
    <h2>View Print Attendee Details in a Tabular form:</h2>
<?php
/** If the button has been clicked */
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    /** Strip Slashes */
    if ( get_magic_quotes_gpc() ) {
        $_POST = array_map( 'stripslashes_deep', $_POST );
    }
    
    /** Convert to date */
    $start_date = strtotime($_POST['start_date']);
    $end_date = strtotime($_POST['end_date']);
    /** Validate */
    if($end_date < $start_date)
        $end_date = $start_date;
    
    /** Fetch Rows */
    $ids = $wpdb->get_col($wpdb->prepare("SELECT uid FROM {$itg_em_db_table_name['reg_table']} WHERE date >= %s AND date <= %s GROUP BY uid", date('Y-m-d', $start_date), date('Y-m-d', $end_date)));
    $count = count($ids);
    if($count) {
        /** IDS there */
        $ids = implode(',', $ids);
        ?>
	<noscript><p><em>You must enable Javascript in order to proceed!</em></p></noscript>
        <div id="message" class="updated fade" style="display:none"></div>
	<div id="itg_em_pgbar" style="position:relative;height:25px;">
		<div id="itg_em_percent" style="position:absolute;left:50%;top:50%;width:50px;margin-left:-25px;height:25px;margin-top:-9px;font-weight:bold;text-align:center;"></div>
	</div>
        <div id="print_wrap" style="display: none;">
            <button id="printable">Print This Table</button>
            <!-- Placeholder for the table -->
            <div id="print">
            <table id="itg_em_atd_printtable" class="widefat">
                <thead>
                    <tr>
                        <th scope="col">User</th>
                        <th scope="col">Event ID - Name</th>
                        <th scope="col">App. ID</th>
                        <th scope="col">Reg No.</th>
                        <th scope="col">Payment - Status</th>
                        <th scope="col">Team Members</th>
                        <th scope="col">Dated</th>
                        <th scope="col">Note:</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <th scope="col">User</th>
                        <th scope="col">Event ID - Name</th>
                        <th scope="col">App. ID</th>
                        <th scope="col">Reg No.</th>
                        <th scope="col">Payment - Status</th>
                        <th scope="col">Team Members</th>
                        <th scope="col">Dated</th>
                        <th scope="col">Note:</th>
                    </tr>
                </tfoot>
                <tbody id="itg_em_atd_table_body">
                    
                </tbody>
            </table>
            <p style="font-size: 8px; font-style: italic">Generated by WP Simple Event management system - Dev. Swashata</p>
            </div>
        </div>
        
        <!-- The JS -->
	<script type="text/javascript">
	// <![CDATA[
		jQuery(document).ready(function($){
			var i;
			var itg_em_atd = [<?php echo $ids; ?>];
			var itg_em_total = itg_em_atd.length;
			var itg_em_count = 1;
			var itg_em_percent = 0;

			$("#itg_em_pgbar").progressbar();
			$("#itg_em_percent").html( "0%" );

			function RegenThumbs( id ) {
				$.post( "admin-ajax.php", { action: "itg_em_list_atd_ajax", id: id }, function(data) {
					itg_em_percent = ( itg_em_count / itg_em_total ) * 100;
					$("#itg_em_pgbar").progressbar( "value", itg_em_percent );
					$("#itg_em_percent").html( Math.round(itg_em_percent) + "%" );
					itg_em_count = itg_em_count + 1;
                                        /** Insert the data */
                                        $('#itg_em_atd_table_body').append(data)
					if ( itg_em_atd.length ) {
						RegenThumbs( itg_em_atd.shift() );
					} else {
						$("#message").html("<p><strong><?php echo esc_js( sprintf( 'All done! Processed %d Records You may now print it.', $count ) ); ?></strong></p>");
						$("#message").show(400);
                                                $('#print_wrap').slideDown(600);
					}
				});
			}

			RegenThumbs( itg_em_atd.shift() );
                        $('#printable').click(function(){
                            $('#print').printArea({mode : 'popup', popClose: false, strict : true});
                        });
		});
	// ]]>
	</script>
        <?php
    }
    else {
        ?>
        <div class="error fade">Sorry, We dont have any registration within your date</div>
        <?php
    }
}
else {
    /**
     * First get the min start date
     * And max end date
     */
    $min_start_date = $wpdb->get_var($wpdb->prepare("SELECT date FROM {$itg_em_db_table_name['reg_table']} ORDER BY date ASC LIMIT 1"));
    $max_end_date = $wpdb->get_var($wpdb->prepare("SELECT date FROM {$itg_em_db_table_name['reg_table']} ORDER BY date DESC LIMIT 1"));
    /** Display the form */
    ?>
    <form action="" method="post">
        <ul>
            <li>
                <label for="start_date">Enter Start Date:</label>
                <input type="text" name="start_date" id="start_date" class="date_field" value="<?php echo $min_start_date; ?>" />
            </li>
            <li>
                <label for="end_date">Enter End Date:</label>
                <input type="text" name="end_date" id="end_date" class="date_field" value="<?php echo $max_end_date; ?>" />
            </li>
            <li>
                <span class="description">The earliest date of registration is: <?php echo $min_start_date; ?> And the recent date of last registration is <?php echo $max_end_date; ?></span>
            </li>
            <li>
                <input type="submit" class="button-primary" value="Get Data" />
            </li>
        </ul>
    </form>
    <?php
}
//$wpdb->show_errors(); $wpdb->print_error();
?>
</div>